﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using B2B.Models;
using System.IO;
using System.Collections;

namespace B2B.Controllers
{
    public class ManageController : AlibabaApplicationController
    {
        //
        // GET: /Manage/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MesagesContacts(string id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            Models.SP_EmailInbox emailInbox = new Models.SP_EmailInbox();
            ViewData["dsMailInbox"] = emailInbox.danhSachMailInBox(Session["email"].ToString());
            Models.SP_SentEmail emailSent = new Models.SP_SentEmail();
            ViewData["dsMailSent"] = emailSent.danSachSentEmail(Session["email"].ToString());
            return View();
        }
        [HttpPost]
        public ActionResult QuoteCreate(HttpPostedFileBase fileUpload, SP_QuoteCreate quoteCreate, int loai)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");

             SP_TimKiemSP_Ma timkiem_SP = new SP_TimKiemSP_Ma();
             string tenFile = "";
            //Loai 1 bao gia co san cua nha cung cap
             if (loai == 1)
             {
                 ViewData["quoteCreate"] = quoteCreate;
                 if (fileUpload != null)
                 {
                     var fileName = Path.GetFileName(fileUpload.FileName);
                     tenFile = fileName;
                     var path = Path.Combine(Server.MapPath("~/Content/Files"), fileName);
                     fileUpload.SaveAs(path);
                     quoteCreate.File = tenFile;
                     quoteCreate.ThemBaoGia(quoteCreate, loai);
                 }
                 ViewData["tenSP"] = timkiem_SP.timKiemSanPhamMa(quoteCreate.MaSP).TenSP;
                 ViewData["loai"] = loai;
             }
                 //Loai 0 bao gia cac san pham nha cung cap tim kiem tren trang web do khach hang cung cap
             else
             {
                 ViewData["quoteCreate"] = quoteCreate;
                 if (fileUpload != null)
                 {
                     var fileName = Path.GetFileName(fileUpload.FileName);
                     tenFile = fileName;
                     var path = Path.Combine(Server.MapPath("~/Content/Files"), fileName);
                     fileUpload.SaveAs(path);
                     quoteCreate.File = tenFile;
                     quoteCreate.ThemBaoGia(quoteCreate, loai);

                 }
                 ViewData["tenSP"] = timkiem_SP.timKiemSanPhamMua(quoteCreate.MaSPM).TenSP;
                 ViewData["loai"] = loai;
             }
            return View();
        }


        public ActionResult QuotesRecived(int id, string emailNhan)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_QuoteRecived quote_Recived = new SP_QuoteRecived();
            ViewData["ds_QuotesRecived"] = quote_Recived.ds_BaoGiaNhan(Session["email"].ToString(), id);
            ViewData["tongtrangNhan"] = quote_Recived.tongTrangBaoGiaNhan(Session["email"].ToString());
            return View();
        }

        public ActionResult QuotesSent(int id, string emailGui)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_QuoteSent quote_Sent = new SP_QuoteSent();
            ViewData["ds_QuotesSent"] = quote_Sent.ds_BaoGiaGui(Session["email"].ToString(), id);
            ViewData["tongtrangGui"] = quote_Sent.tongTrangBaoGiaGui(Session["email"].ToString());
            return View();
        }

        public ActionResult QuotesSentPostBuy(int id)
        {
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_QuoteSent quote_Sent = new SP_QuoteSent();
            ViewData["ds_QuotesSent_SPM"] = quote_Sent.danhSachBaoGiaSPMGui(id, Session["email"].ToString());
            ViewData["tongtrang_guiSPM"] = quote_Sent.tongTrangBaoGiaGuiSPM(Session["email"].ToString());
            return View();
        }

        public ActionResult QuotesRecivedPostBuy(int id)
        {
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_QuoteRecived quotes_Recived = new SP_QuoteRecived();
            ViewData["ds_QuotesRecived_SPM"] = quotes_Recived.danhSachBaoGiaSPMNhan(id, Session["email"].ToString());
            ViewData["tongtrang_nhanSPM"] = quotes_Recived.tongTrangBaoGiaNhanSPM(Session["email"].ToString());
            return View();
        }
    

        public JsonResult DeleteQuote(int maBaoGia)
        {
            int kq = 0;
            //Kiem tra session
            if (Session["email"] == null)
                return Json(0);
            kq = SP_XoaBaoGia.xoaBaoGia(maBaoGia);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateQuoteRecived(int id, int tinhTrang)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_CapNhatBaoGia.capNhatBaoGia(id, tinhTrang);
            return View();
        }

        public ActionResult OrdersSent()
        {
            return View();
        }

        public ActionResult OdersReciver(int id, string emailNhan)
        {
         //   SP_OrdersReciver orders = new SP_OrdersReciver();
      //      ViewData["list_Orders"] = orders.ds_DonHangNhan(emailNhan, id);
            return View();
        }

        public ActionResult OrdersSent(int id, string emailGui)
        {
       //     SP_OrdersSent orders = new SP_OrdersSent();
       //     ViewData["list_Orders"] = orders.ds_DonHangGui(emailGui, id);
            return View();
        }
             

        public ActionResult ManageBuyingRequest(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            PostBuyRequest spm = new PostBuyRequest();
            ViewData["list_SPM"] = spm.DanhSachSanPhamMua_MaTV(int.Parse(Session["maTV"].ToString()), id);
            ViewData["tongTrangPostbuy"] = spm.tongSoTrangPostBuy(int.Parse(Session["maTV"].ToString()));
            return View();
        }

        public JsonResult ProductDeletePostBuyRequest(int id)
        {
            int kq = 0;
            if (Session["email"]==null)
            {
                return Json(0);
            } 
            PostBuyRequest pbr = new PostBuyRequest();
                kq = pbr.XoaSanPhamMua(id);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ProductUpdatePostBuyRequest(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
 
            Models.LinhVuc lv = new Models.LinhVuc();
            ViewData["linhvuc"] = lv.DanhSachLinhVuc();
            PostBuyRequest prb = LaySanPhamMua(id);
            ViewData["SPMua"] = prb;
            DanhMuc dm = new DanhMuc();
            ViewData["DM_SPM"] = dm.DanhSachDanhMuc(prb.MaLinhVuc);

            return View();
        }

         [ValidateInput(false)]
        public ActionResult UpdatePostBuy(PostBuyRequest prb, HttpPostedFileBase fileUpload)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");

            var fileName = prb.HinhAnh;
            var path= "";
            if (fileUpload != null)
            {
                fileName = Path.GetFileName(fileUpload.FileName);
                path = Path.Combine(Server.MapPath("~/Images/Product/"), fileName);
                fileUpload.SaveAs(path);
            }
            
            ViewData["SPMua"] = prb;
             Models.LinhVuc lv = new Models.LinhVuc();
            ViewData["linhvuc"] = lv.DanhSachLinhVuc();
              DanhMuc dm = new DanhMuc();
            ViewData["DM_SPM"] = dm.DanhSachDanhMuc(prb.MaLinhVuc);
            prb.CapNhatSanPhamMua(prb, fileName, prb.MaSanPhamMua);

            return View("ProductUpdatePostBuyRequest");
        }

        

        public PostBuyRequest LaySanPhamMua(int id)
        {
            Models.PostBuyRequest pbr = new Models.PostBuyRequest();
            return pbr.LaySanPhamMua(id);            
        }

        public ActionResult ProductsManagement()
        {
            int tongpage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();

            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPhamUserQuanLy(1, out tongpage, int.Parse(Session["maTV"].ToString()));
            ViewData["tongPage"] = tongpage;
            ViewData["listSP"] = lis_SP;

            Models.LinhVuc lv = new Models.LinhVuc();
            ViewData["linhvuc"] = lv.DanhSachLinhVuc();
            return View();
        }

        public JsonResult ProductList(int page)
        {
            int tongPage = 0 ;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList list_SP = sp_DanhSachSanPham.DanhSachSanPhamUserQuanLy(page, out tongPage, int.Parse(Session["matv"].ToString()));
            ViewData["tongPage"] = tongPage;
            return Json(list_SP, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ListDetailProduct(int madm)
        {
            ThongTinSanPham ttsp = new ThongTinSanPham();
            ArrayList listTTSP = ttsp.danhSachThongTinSanPham(madm);
            return Json(listTTSP, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CreateProduct()
        {
            Models.LinhVuc lv = new Models.LinhVuc();
            DonViTinh dvt = new DonViTinh();
            QuocGia qg = new QuocGia();
            ViewData["donvitinh"]= dvt.DanhSachDonViTinh(); 
            ViewData["linhvuc"] = lv.DanhSachLinhVuc();
            ViewData["quocgia"] = qg.DanhSachQuocGia();
            return View();
        }

        public ActionResult ProfileManagement()
        {
            return View();
        }

        public ActionResult AddProduct(SanPham sp, HttpPostedFileBase fileUpload, List<ThongTinSanPham> ttsps)
        {
            SanPham spThem = new SanPham();

            if (fileUpload != null)
            {
                var fileName = Path.GetFileName(fileUpload.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/Product/"), fileName);
                fileUpload.SaveAs(path);
                sp.HinhAnh = fileName;
                sp.MaTV = int.Parse(Session["maTV"].ToString());
                int maSP = spThem.ThemSanPham(sp);
                ThongTinSanPham ttsp = new ThongTinSanPham();
                int count = ttsps.Count;
                for (int i = 0; i < count; i++)
                {
                    ttsps[i].MaSP = maSP;
                    ttsp.ThemChiTietSanPham(ttsps[i]);
                }
            }


           
            return View();
        }

        public ActionResult CreateOrder(int id, SP_ThemDonHang donHang)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_TimKiemBaoGia_Ma timKiemBaoGia = new SP_TimKiemBaoGia_Ma();
            ViewData["baoGia"] = timKiemBaoGia.timKiemBaoGia(id);
            if (donHang.SoLuongDat != 0)
            {
                donHang.ThemDonHang(donHang);
            }
            return View();
        }

        public ActionResult PaymentOrder(int id)
        {
            if (Session["maTV"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_LayDonHang donhang = new SP_LayDonHang();
            ViewData["donhang"] = donhang.layDonDatHang(id);
            return View();
        }

        public ActionResult OrdersManagement(int id, int pageNhan)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_OrdersSent orders_Sent = new SP_OrdersSent();
            ViewData["list_Orders_Sent"] = orders_Sent.ds_DonHangGui(Session["email"].ToString(), id);
            ViewData["tongTrangGui"] = orders_Sent.sp_TongTrangDHGui(Session["email"].ToString());
            SP_OrdersReciver orders_Recived = new SP_OrdersReciver();
            ViewData["list_Orders_Recived"] = orders_Recived.ds_DonHangNhan(Session["email"].ToString(), pageNhan);
            ViewData["tongTrangNhan"] = orders_Recived.sp_TongTrangDHNhan(Session["email"].ToString());
            return View();
        }

        public JsonResult ordersSentJson(int page)
        {
            if (Session["email"] == null)
                return Json("NOt");
            SP_OrdersSent orders_Sent = new SP_OrdersSent();
            ArrayList list_OrdersSent = orders_Sent.ds_DonHangGui(Session["email"].ToString(), page);
            return Json(list_OrdersSent, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ordersReciveJson(int page)
        {
            if (Session["email"] == null)
                return Json("NOt");
            SP_OrdersReciver orders_Recived = new SP_OrdersReciver();
            ArrayList list_OrdersRecive = orders_Recived.ds_DonHangNhan(Session["email"].ToString(), page);
            return Json(list_OrdersRecive, JsonRequestBehavior.AllowGet);
        }

        public JsonResult orderDetail_Product(int maDH)
        {
            if (Session["email"] == null)
                return Json("NOt");
            SP_ChiTietDonHang_SP sp_DH_SP = new SP_ChiTietDonHang_SP();
            sp_DH_SP = sp_DH_SP.SP_ChiTietDH_SP(maDH);
            return Json(sp_DH_SP, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateOrder(int maDH, int tinhTrang)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            SP_CapNhatDonHang.capNhatDonHang(maDH, tinhTrang);
            return View();
        }

        public ActionResult UpdateProduct(int maSP)
        {
            Models.LinhVuc lv = new Models.LinhVuc();
            ViewData["linhvuc"] = lv.DanhSachLinhVuc();
            return View();
        }

        public JsonResult DeleteProduct(int maSP)
        {
            SanPham sp = new SanPham();

            int kq = sp.XoaSanPham(maSP);
            if (kq == 1)
            {
                int tongpage = 0;
                SP_DanhSachSanPham list_sp = new SP_DanhSachSanPham();
                ArrayList listSP = list_sp.DanhSachSanPhamUserQuanLy(1, out tongpage, int.Parse(Session["maTV"].ToString()));
                return Json(list_sp, JsonRequestBehavior.AllowGet);
            }
            return Json("fail", JsonRequestBehavior.AllowGet);
        }



        public JsonResult DeleteOrder(int maDH)
        {
            int kq = 0;
            //Kiem tra session
            if (Session["email"] == null)
                return Json(0);    
            kq = SP_XoaDonHang.xoaDonHang(maDH);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

       

        public ActionResult MessageDetail(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            Models.SP_MessageDetail messageDetail = new Models.SP_MessageDetail();
            ViewData["messageDetail"] = messageDetail.messageDetail(id);
            return View();
        }

       

        public ActionResult MessageInbox(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            Models.SP_MessageDetail messageDetail = new Models.SP_MessageDetail();
            ViewData["messageDetail"] = messageDetail.messageDetail(id);
            return View();
        }

        public ActionResult MessageSentBox(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            Models.SP_MessageDetail messageDetail = new Models.SP_MessageDetail();
            ViewData["messageDetail"] = messageDetail.messageDetail(id);
            return View();
        }

       /* public ActionResult DeleteMessage(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            Models.SP_DeleteMessage deleteMessage = new Models.SP_DeleteMessage();
            int kq = deleteMessage.deleteMessage(id);
            return RedirectToAction("MesagesContacts");
        }*/

        public JsonResult DeleteMessage(int maMessage)
        {
            if (Session["email"] == null)
                return Json(0);
            Models.SP_DeleteMessage deleteMessage = new Models.SP_DeleteMessage();
            int kq = deleteMessage.deleteMessage(maMessage);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
    }
}
